package com.flyee.viewer
{
	import com.flyee.controller.MonsterController;
	import com.flyee.model.Charactor;
	import com.flyee.model.Monster;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.GCHleper;
	import com.flyee.viewer.pack.helper.CellGridBox;
	import com.flyee.viewer.pack.helper.ItemDataModel;
	import com.flyee.viewer.pack.helper.ItemHelper;
	import com.flyee.viewer.pack.helper.ItemViewModel;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.controls.Text;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	
	public class PickUpItemViewer extends BaseWindowViewer
	{
		private var cellArray:Array = null;
		[Embed(source="resource/redDrug.jpg")]
		private var RedDrug:Class;
		private var monster:Charactor = null;
		public function PickUpItemViewer()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, initEvent);
			this.addEventListener(CloseEvent.CLOSE, closePanelEvent);
			this.showCloseButton=true;
			//this.layout = "absolute";
			this.setStyle("paddingLeft",5);
			this.setStyle("paddingRight",5);
			this.setStyle("paddingBottom",5);
			this.setStyle("paddingTop",5);
			this.setStyle("borderThicknessLeft",2);
			this.setStyle("borderThicknessRight",2);
			this.setStyle("borderThicknessTop",0);
			this.setStyle("borderThicknessBottom",2);
			this.verticalScrollPolicy = "on";
			this.setStyle("verticalGap",5);
			//this.setStyle("horizontalAlign","center");
			//this.setStyle("horizontalGap",43);
			cellArray = new Array();
		}
		
		private function initEvent(event:Event):void
		{
			this.width = 150;
			this.height = 200;
			this.canDragMove = true;
		}
		
		private function closePanelEvent(event:Event):void
		{
			if (ObjectCollection.uiContainer.contains(this))
			{
				ObjectCollection.uiContainer.removeChild(this);
			}
			dispose();
		}
		
		private function itemBar(item:ItemViewModel,mess:String):HBox{
			var hbox:HBox = new HBox();
			var cell:CellGridBox = new CellGridBox();
			var label:Text = new Text();
			hbox.setStyle("horizontalGap",43);
			hbox.verticalScrollPolicy = "off";
			hbox.horizontalScrollPolicy = "off";
			label.percentHeight = 100;
			label.width = 60;
			label.selectable = false;
			label.setStyle("textAlign","left");
			label.text = item.dataModel.name;
			hbox.height = 40;
			cell.addProp(item);
			hbox.addChild(cell);
			hbox.addChild(label);
			hbox.toolTip = mess;
			hbox.addEventListener(MouseEvent.CLICK,pickUpItemEvent);
			return hbox;
		}
		
		private function pickUpItemEvent(event:Event):void{
			var hbox:HBox = event.currentTarget as HBox;
			var cell:CellGridBox = hbox.getChildAt(0) as CellGridBox;
			var label:Text = hbox.getChildAt(1) as Text;
			var item:ItemViewModel = cell.absProp;
			if(ObjectCollection.packPanelViewer){
				ObjectCollection.packPanelViewer.updateItem(item);
			}
			var itemData:ItemDataModel = ObjectCollection.skillPanelViewer.drugItems.getItemAt(0) as ItemDataModel;
			itemData.capability = itemData.currentCapability+1;
			itemData.action = "add";
			ObjectCollection.skillPanelViewer.drugItems.refresh();
			if(this.monster){
				var monsterController:MonsterController = 
									ObjectCollection.beansContext.getObject("monsterController") as MonsterController;
				monsterController.disposeDead(this.monster as Monster);
			}			
			cell.removeData();
			label.text = "";
			if (ObjectCollection.uiContainer.contains(this))
			{
				ObjectCollection.uiContainer.removeChild(this);
			}
			dispose();
		}
		
		public function get cells():Array{
			return cellArray;
		}
		
		public function fill(target:Charactor,items:Array):void{
			this.monster = target;
			for(var i:int = 0;i < items.length;i++){
				var equip:Bitmap=null;
				equip = new RedDrug();
				var item:ItemViewModel=ItemHelper.cloneProp(items[i],false,equip.bitmapData);
				item.isDrag = false;
				var hbox:HBox = itemBar(item,item.dataModel.message);
				cellArray.push(hbox);	
				this.addChild(hbox);
			}
		}

		override public function dispose():void
		{
			GCHleper.clearAll(this);
			cellArray.splice(0);
			cellArray = null;
		}
		
	}
}